package com.travelranking.view;

import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.apache.commons.lang.StringUtils;

import com.travelranking.pojo.Country;
import com.travelranking.service.CountryService;

public class CountryAction {

	private Country c;
	private CountryService service;
	private Long CountryID;
	
	
	public Long getCountryID() {
		return CountryID;
	}

	public void setCountryID(Long countryID) {
		CountryID = countryID;
	}

	public CountryAction(){
		c = new Country();
	}
	
	private boolean validateFields(){
		boolean result = false;
		
		if (StringUtils.isEmpty(c.getName())){
			addFacesMessage("Nome do país deve ser informado.");
		}else{
			result = true;
		}
		return result;
	}
	
	public void setListCountry(List<SelectItem> l){}
	public List<SelectItem> getListCountry(){
		List<SelectItem> result = new ArrayList<SelectItem>();
		for (Country item : service.list()){
			result.add(new SelectItem(item.getId(), item.getName()));
		}
		return result;
	}
	
	public String add(){
		String result = "NEWCOUNTRY";
		
		if (validateFields()){
			service.add(c);
			result = "LIST";
		}
		return result;
	}
	
	public String cleanAndList(){

		return "LIST";
	}
	public String cleanFields(){
		c = new Country();
		return "NEWCOUNTRY";
	}
	
	public Country getC() {
		return c;
	}
	public void setC(Country c) {
		this.c = c;
	}

	/*
	 * Metodos de acesso ao service manager
	 */
	public CountryService getService() {
		return service;
	}
	public void setService(CountryService service) {
		this.service = service;
	}
	
	public void addFacesMessage(String s){
		FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO,s,s);
		FacesContext cont = FacesContext.getCurrentInstance();
		cont.addMessage(null, msg);
	}
}
